/**
 * @file time.h
 * COPYRIGHT (C) 2022,  chengmeng_2@outlook.com Co., Ltd.
 * All rights reserved.
 * @brief bsp定时器组件
 * @details C51 定时实现滴答时钟的设计
 *
 * @version ver1.0
 * @date 2022年5月1日
 * @author cmm
 * @note
 */
#include "SysTick.h"

static uint32_t countMS = 0;

void SysTickInit()
{
#if SYSTICK_SELECT == TIME2
#if TIME_MODE == TIME_MODE_12T
    AUXR &= ~0x04; // 定时器2为12T模式
#else
    AUXR |= 0x04; // 定时器2为1T模式
#endif

    T2L = SYSTICK_FREQ;      // initial timer2 low byte
    T2H = SYSTICK_FREQ >> 8; // initial timer2 high byte
    AUXR |= 0x10;            // 定时器2开始计时
    IE2 |= 0x04;             // enable timer2 interrupt
    EA = 1;                  // open global interrupt switch

#else
#error "please select one of the   timer"
#endif
}

//-----------------------------------------------
#if SYSTICK_SELECT == TIME2
/* Timer2 interrupt routine */
void tm2_isr() interrupt 12 using 1
{
    countMS++;
}
#endif
uint32_t GetTick()
{
    return countMS;
}
/**
 * @brief 获取滴答时钟计数
 */
void Tick_Delayms(uint16_t Delay)
{
    uint32_t tickstart = GetTick();
    uint32_t wait = Delay;
    while ((GetTick() - tickstart) < wait)
    {
    }
}
